package com.bn.feeds;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bn.feeds.tools.FeedRepositoryTool;

import atg.core.util.StringUtils;
import atg.nucleus.GenericService;
import atg.nucleus.ServiceException;
import atg.repository.MutableRepository;
import atg.repository.Repository;



/**
 * The Class MasterFeedConfiguration.
 * @author
 */
public class MasterFeedConfiguration extends GenericService {

	/** The file path. */
	private String filePath;
	
	private String processedPath;
	
	private String deltaArchivePath;

	/** Error File Directory**/
	private String errorDirectoryPath;

	/** The prefix. */
	private String prefix;

	/** The file type. */
	private String fileType;

	/** The token property map. */
	private Map<String, String> tokenPropertyMap;

	/** The token item property map. */
	private Map<String, String> tokenItemPropertyMap;

	/** The repository. */
	private MutableRepository repository;

	/** The item descriptor name. */
	private String itemDescriptorName;

	/** The repository tools. */
	private FeedRepositoryTool repositoryTools;

	/** The repository id column. */
	private String repositoryIdColumn;

	/** The enable. */
	private boolean enable;

	/** The prop data type map. */
	private Map<String, String> propDataTypeMap;

	/** The mandatory properties. */
	private List<String> mandatoryProperties;

	/** The default property. */
	private String defaultProperty;

	/** The truncate data. */
	private boolean truncateData=false;

	/** The archive location. */
	private String archiveLocation ;

	/** The use ootb id. */
	private boolean useOOTBId = false;

	/** batch size */
	private int batchSize =-1;

	/** If market feed*/
	private boolean emailMarketFeed = false;
	
	private boolean noHeader=false;

	/** mandatory properties to be present in header of feed file**/
	private List<String> mandatoryHeaderProperties;
	private Repository prdRepository;

	public Repository getPrdRepository() {
		return prdRepository;
	}

	public void setPrdRepository(Repository prdRepository) {
		this.prdRepository = prdRepository;
	}

	/**
	 * Gets the file path.
	 *
	 * @return the file path
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * Sets the file path.
	 *
	 * @param filePath
	 *            the new file path
	 */
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	/**
	 * Gets the prefix.
	 *
	 * @return the prefix
	 */
	public String getPrefix() {
		return prefix;
	}

	/**
	 * Sets the prefix.
	 *
	 * @param prefix
	 *            the new prefix
	 */
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	/**
	 * Gets the file type.
	 *
	 * @return the file type
	 */
	public String getFileType() {
		return fileType;
	}

	/**
	 * Sets the file type.
	 *
	 * @param fileType
	 *            the new file type
	 */
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	/**
	 * Gets the token property map.
	 *
	 * @return the token property map
	 */
	public Map<String, String> getTokenPropertyMap() {
		return tokenPropertyMap;
	}

	/**
	 * Sets the token property map.
	 *
	 * @param tokenPropertyMap
	 *            the token property map
	 */
	public void setTokenPropertyMap(Map<String, String> tokenPropertyMap) {
		this.tokenPropertyMap = tokenPropertyMap;
	}

	/**
	 * Gets the token item property map.
	 *
	 * @return the token item property map
	 */
	public Map<String, String> getTokenItemPropertyMap() {
		return tokenItemPropertyMap;
	}

	/**
	 * Sets the token item property map.
	 *
	 * @param tokenItemPropertyMap
	 *            the token item property map
	 */
	public void setTokenItemPropertyMap(Map<String, String> tokenItemPropertyMap) {
		this.tokenItemPropertyMap = tokenItemPropertyMap;
	}

	/**
	 * Gets the repository.
	 *
	 * @return the repository
	 */
	public MutableRepository getRepository() {
		return repository;
	}

	/**
	 * Sets the repository.
	 *
	 * @param repository
	 *            the new repository
	 */
	public void setRepository(MutableRepository repository) {
		this.repository = repository;
	}

	/**
	 * Gets the item descriptor name.
	 *
	 * @return the item descriptor name
	 */
	public String getItemDescriptorName() {
		return itemDescriptorName;
	}

	/**
	 * Sets the item descriptor name.
	 *
	 * @param itemDescriptorName
	 *            the new item descriptor name
	 */
	public void setItemDescriptorName(String itemDescriptorName) {
		this.itemDescriptorName = itemDescriptorName;
	}

	/**
	 * Gets the repository tools.
	 *
	 * @return the repository tools
	 */
	public FeedRepositoryTool getRepositoryTools() {
		return repositoryTools;
	}

	/**
	 * Sets the repository tools.
	 *
	 * @param repositoryTools
	 *            the new repository tools
	 */
	public void setRepositoryTools(FeedRepositoryTool repositoryTools) {
		this.repositoryTools = repositoryTools;
	}

	/**
	 * Gets the repository id column.
	 *
	 * @return the repository id column
	 */
	public String getRepositoryIdColumn() {
		return repositoryIdColumn;
	}

	/**
	 * Sets the repository id column.
	 *
	 * @param repositoryIdColumn
	 *            the new repository id column
	 */
	public void setRepositoryIdColumn(String repositoryIdColumn) {
		this.repositoryIdColumn = repositoryIdColumn;
	}

    /**
	 * Checks if is enable.
	 *
	 * @return true, if is enable
	 */
    public boolean isEnable() {
        return enable;
    }

    /**
	 * Sets the enable.
	 *
	 * @param enable
	 *            the new enable
	 */
    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    /**
	 * Gets the prop data type map.
	 *
	 * @return the prop data type map
	 */
    public Map<String, String> getPropDataTypeMap() {
        return propDataTypeMap;
    }

    /**
	 * Sets the prop data type map.
	 *
	 * @param propDataTypeMap
	 *            the prop data type map
	 */
    public void setPropDataTypeMap(Map<String, String> propDataTypeMap) {
        this.propDataTypeMap = propDataTypeMap;
    }

    /**
	 * Gets the mandatory properties.
	 *
	 * @return the mandatory properties
	 */
    public List<String> getMandatoryProperties() {
        return mandatoryProperties;
    }

    /**
	 * Sets the mandatory properties.
	 *
	 * @param mandatoryProperties
	 *            the new mandatory properties
	 */
    public void setMandatoryProperties(List<String> mandatoryProperties) {
        this.mandatoryProperties = mandatoryProperties;
    }

    /**
	 * Gets the default property.
	 *
	 * @return the default property
	 */
    public String getDefaultProperty() {
        return defaultProperty;
    }

    /**
	 * Sets the default property.
	 *
	 * @param defaultProperty
	 *            the new default property
	 */
    public void setDefaultProperty(String defaultProperty) {
        this.defaultProperty = defaultProperty;
    }

    /**
	 * Checks if is truncate data.
	 *
	 * @return true, if is truncate data
	 */
    public boolean isTruncateData() {
        return truncateData;
    }

    /**
	 * Sets the truncate data.
	 *
	 * @param truncateData
	 *            the new truncate data
	 */
    public void setTruncateData(boolean truncateData) {
        this.truncateData = truncateData;
    }

    /**
	 * Gets the archive location.
	 *
	 * @return the archive location
	 */
    public String getArchiveLocation() {
        return archiveLocation;
    }

    /**
	 * Sets the archive location.
	 *
	 * @param archiveLocation
	 *            the new archive location
	 */
    public void setArchiveLocation(String archiveLocation) {
        this.archiveLocation = archiveLocation;
    }

    /**
	 * Checks if is use ootb id.
	 *
	 * @return true, if is use ootb id
	 */
    public boolean isUseOOTBId() {
        return useOOTBId;
    }

    /**
	 * Sets the use ootb id.
	 *
	 * @param useOOTBId
	 *            the new use ootb id
	 */
    public void setUseOOTBId(boolean useOOTBId) {
        this.useOOTBId = useOOTBId;
    }

    /**
     * @return the errorDirectoryPath
     */
    public String getErrorDirectoryPath() {
        return errorDirectoryPath;
    }

    /**
     * @param errorDirectoryPath the errorDirectoryPath to set
     */
    public void setErrorDirectoryPath(String errorDirectoryPath) {
        this.errorDirectoryPath = errorDirectoryPath;
    }

    /* (non-Javadoc)
     * @see atg.nucleus.GenericService#doStartService()
     */
    @Override
    public void doStartService() throws ServiceException {
        super.doStartService();
        if (propDataTypeMap == null){
            propDataTypeMap = new HashMap<String, String>();
        }
        correctMap(tokenPropertyMap, true, false);
        correctMap(tokenItemPropertyMap, true, false);
        correctMap(propDataTypeMap, false, false);
        if(!StringUtils.isBlank(filePath)){
        	filePath = filePath.trim();
        }
        if(!StringUtils.isBlank(prefix)){
        	prefix = prefix.trim();
        }
        if(!StringUtils.isBlank(fileType)){
        	fileType = fileType.trim();
        }
        if(!StringUtils.isBlank(repositoryIdColumn)){
        	repositoryIdColumn = repositoryIdColumn.trim();
        }
        if(!StringUtils.isBlank(itemDescriptorName)){
        	itemDescriptorName = itemDescriptorName.trim();
        }

        if (batchSize <= 0) {
            batchSize = -1;
        }
    }

    /**
	 * Correct map.
	 *
	 * @param dataMap
	 *            the data map
	 * @param isKeyToLowercase
	 *            the is key to lowercase
	 * @param isValToLowercase
	 *            the is val to lowercase
	 */
    private void correctMap(final Map<String, String> dataMap, final boolean isKeyToLowercase, final boolean isValToLowercase) {

        if ((dataMap != null) && (!dataMap.isEmpty())){
            Map<String, String> tempMap = new HashMap<String, String>();
            Set<Map.Entry<String, String>> entries =  dataMap.entrySet();

            for (Map.Entry<String, String> entry: entries){

                String key = isKeyToLowercase ? entry.getKey().toLowerCase().trim() :entry.getKey().trim() ;
                String value = entry.getValue().trim();
                if (!StringUtils.isBlank(value)){
                    value = isValToLowercase ? value.toLowerCase() : value ;
                }
                tempMap.put(key, value);
            }

            entries.clear();
            dataMap.putAll(tempMap);
        }

    }

    /**
     * @return the batchSize
     */
    public int getBatchSize() {
        return batchSize;
    }

    /**
     * @param batchSize the batchSize to set
     */
    public void setBatchSize(int batchSize) {
        this.batchSize = batchSize;
    }

    /**
     * @return the mandatoryHeaderProperties
     */
    public List<String> getMandatoryHeaderProperties() {
        return mandatoryHeaderProperties;
    }

    /**
     * @param mandatoryHeaderProperties the mandatoryHeaderProperties to set
     */
    public void setMandatoryHeaderProperties(List<String> mandatoryHeaderProperties) {
        this.mandatoryHeaderProperties = mandatoryHeaderProperties;
    }

	public boolean isEmailMarketFeed() {
		return emailMarketFeed;
	}

	public void setEmailMarketFeed(boolean emailMarketFeed) {
		this.emailMarketFeed = emailMarketFeed;
	}

	public boolean isNoHeader() {
		return noHeader;
	}

	public void setNoHeader(boolean noHeader) {
		this.noHeader = noHeader;
	}

	public String getProcessedPath() {
		return processedPath;
	}

	public void setProcessedPath(String processedPath) {
		this.processedPath = processedPath;
	}

	public String getDeltaArchivePath() {
		return deltaArchivePath;
	}

	public void setDeltaArchivePath(String deltaArchivePath) {
		this.deltaArchivePath = deltaArchivePath;
	}


}
